package com.cms.recursoshumanos.component.base;

import javax.microedition.lcdui.Image;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;

public class CustomMenuField extends Field implements DrawStyle {
   private String _label;
   private Bitmap _image, _image_focus;
   private Font _font;
   private int _labelHeight;
   private int _labelWidth;
   private int _widthTotal;
   private int backgroundColour;
   private int backgroundFocusColour;
   private int fontColour;
   
   private static final int COLOR_BACKGROUND_FOCUS = 0xFFFFFF;
   private static final int COLOR_BACKGROUND = 0xFFFFFF;
   private static final int COLOR_FONT = 0x000000;
   
   public CustomMenuField(String label, Bitmap image) {
	   this(label, image, (long) 0);
   }
   public CustomMenuField(String label, Bitmap image, Font font) {
	   this(label, image, (long) 0, font);
   }
   public CustomMenuField(String label, Bitmap image, long style) {
	   super(Field.FOCUSABLE|style);
	   _label=label;
	   _image=image;
	   _font = getFont();
	   _labelHeight = _font.getHeight();
	   _labelWidth = _font.getAdvance(_label);
	   backgroundColour = COLOR_BACKGROUND;
	   backgroundFocusColour = COLOR_BACKGROUND_FOCUS;
	   fontColour = COLOR_FONT;
	   _widthTotal = Math.max(_image.getWidth(),_labelWidth)+10;
   }
   public CustomMenuField(String label, Bitmap image, Bitmap image_focus, Font font, long style) {
	   super(Field.FOCUSABLE|style);
	   _label=label;
	   _image=image;
	   _image_focus=image_focus;
	   _font = font;
	   _labelHeight = _font.getHeight();
	   _labelWidth = _font.getAdvance(_label);
	   backgroundColour = COLOR_BACKGROUND;
	   backgroundFocusColour = COLOR_BACKGROUND;
	   fontColour = COLOR_FONT;
	   _widthTotal = Math.max(_image.getWidth(),_labelWidth)+10;
   }
   public int getHeightText()
   {
	   
	   int modulo;
	   int division;
	   if (_labelWidth > _image.getWidth())
	   {
		    modulo = _labelWidth%_image.getWidth();
		    division = _labelWidth/_image.getWidth();
		    if (modulo != 0)
		    {
		    	division =  division+1;
		    }
		    
	   }
	   else
	   {
		   division = _font.getHeight();
	   }
	   
	   
	   return division;
   }
   public CustomMenuField(String label, Bitmap image, long style, Font font, int colorBackHightlight) {
	   this(label, image, style, font, colorBackHightlight, COLOR_BACKGROUND, COLOR_FONT);
   }
   public CustomMenuField(String label, Bitmap image, long style, Font font) {
	   this(label, image, style, font, COLOR_BACKGROUND_FOCUS, COLOR_BACKGROUND, COLOR_FONT);
   }   
   public CustomMenuField(String label, Bitmap image, long style, Font font, int colorBackHighlight, int colorBackground, int colorFont) {
	   this(label, image, style, font, colorBackHighlight, colorBackground, colorFont, Math.max(image.getWidth(),font.getAdvance(label))+10 );
   }   
   public CustomMenuField(String label, Bitmap image, long style, Font font, int colorBackHighlight, int colorBackground, int colorFont, int width) {
	   super(Field.FOCUSABLE|style);
	   _label=label;
	   _image=image;
	   _font = font;
	   _labelHeight = _font.getHeight();
	   _labelWidth = _font.getAdvance(_label);
	   backgroundFocusColour = colorBackHighlight;
	   backgroundColour = colorBackground;
	   fontColour = colorFont;
	   _widthTotal = width;
   } 
   public int getPreferredWidth() {
	   return Math.max(_widthTotal,Math.max(_image.getWidth(),_labelWidth)+10);
   }
   public int getPreferredHeight() {
	   return _image.getHeight()+_labelHeight+10;
   }
   protected void layout(int width, int height) {
	   setExtent(getPreferredWidth(), getPreferredHeight());
   }
   protected void paint(Graphics graphics) {
	   int textX, textY, textWidth;
	   int w = getWidth();
	   
       int imageX = (w/2)-(_image.getWidth()/2);
	   
       if(isFocus()){
    	   graphics.setBackgroundColor(backgroundFocusColour); 
       }else{
    	   graphics.setBackgroundColor(backgroundColour); 
       }

	   graphics.clear();
       
       graphics.setColor(fontColour);
       if(isFocus() && _image_focus != null){
    	   graphics.drawBitmap( imageX, 2, _image_focus.getWidth(), _image_focus.getHeight(), _image_focus, 0, 0 );
       } else {
    	   graphics.drawBitmap( imageX, 2, _image.getWidth(), _image.getHeight(), _image, 0, 0 );
       }
       //graphics.drawRect(imageX, 2, _image.getWidth(), _image.getHeight());
       textX = 4;
       textY = _image.getHeight()+5;
       textWidth = w - 6;
	   graphics.setFont(_font);
       graphics.drawText(_label, textX, textY, (int)( getStyle() & DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK ), textWidth);
       
   	}
}
